package top.saplf.sileny.editor.home

import android.support.v4.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.jakewharton.rxbinding2.view.clicks
import com.jakewharton.rxbinding2.view.longClicks
import kotlinx.android.synthetic.main.item_editor_index.view.*
import top.saplf.sileny.R
import java.text.SimpleDateFormat

/**
 * @author saplf
 */
class EditorIndexAdapter : BaseQuickAdapter<Note, BaseViewHolder>(R.layout.item_editor_index) {
    companion object {
        private val dateFormatter: SimpleDateFormat by lazy {
            SimpleDateFormat("'修改时间：'yyyy-MM-dd HH:mm:ss")
        }
    }

    var itemAction: ((Note) -> Unit)? = null
    var longItemAction: ((Note) -> Unit)? = null

    override fun convert(helper: BaseViewHolder, item: Note) {
        val titleText = if (item.title.isNullOrEmpty()) "(暂无标题)" else item.title
        with(helper.itemView) {
            title.text = titleText
            title.setTextColor(ContextCompat.getColor(context, if (item.title.isNullOrEmpty()) android.R.color.darker_gray else android.R.color.black))
            time.text = dateFormatter.format(item.createTime)

            rootView.clicks()
                    .subscribe { itemAction?.invoke(item) }

            rootView.longClicks()
                    .subscribe { longItemAction?.invoke(item) }
        }
    }
}